/*
 * Created by SharpDevelop.
 * User: K 3 N ! L
 * Date: 11/22/2008
 * Time: 7:07 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Collections;
using System.Data.OleDb;
using System.Xml;
using System.Xml.Xsl;
using System.Diagnostics;

namespace ChallanSystem
{
	/// <summary>
	/// Description of ViewOrderProducts.
	/// </summary>
	public partial class ViewOrderProducts : Form
	{
		XmlDocument xdoc = null;

		public DataRow dr = null;
		public DataSet dsOrderHistory = null;
		string[] orderProductsDetails = null;
		
		private static string strCon = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=./Data/DB.mdb" ;
		private OleDbConnection myCon = new OleDbConnection(strCon);
		
		public ViewOrderProducts()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		public void RefreshDataGrid(string[] orderProductsDetailsArray)
		{
			this.orderProductsDetails = orderProductsDetailsArray;
			//MessageBox.Show("Customer name : "+order_id);
			dsOrderHistory = View(Convert.ToInt32(orderProductsDetailsArray[0]));
			datagridOrders.SetDataBinding(dsOrderHistory, "ViewOrderProd");
		}
		
		public DataSet View(int orderId)
		{
			string sqlStr = "SELECT ORD_PROD.PRODUCT_ID, PROD.PRODUCT_NAME, ORD_PROD.QUANTITY, ORD_PROD.PRICE, ORD_PROD.QUANTITY_PRICE FROM ORDER_PRODUCTS AS ORD_PROD, ORDERS AS ORD, PRODUCTS AS PROD WHERE ORD.ORDER_ID=ORD_PROD.ORDER_ID And ORD.ORDER_ID="+orderId+" And PROD.PRODUCT_ID=ORD_PROD.PRODUCT_ID";
			//Make a DataSet object
			DataSet myDataSet = new DataSet();
			//Using the OleDbDataAdapter execute the query
			//MessageBox.Show(sqlStr);
			OleDbDataAdapter myAdapter = new OleDbDataAdapter( sqlStr , myCon );
			//Fill the DataSet with the Table 'Product'
			myAdapter.Fill(myDataSet,"ViewOrderProd");
			return myDataSet;
		}
		public void generateXML(string[] orderProdDetails)
		{
			string xmlstr = string.Empty;
			xmlstr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
			xmlstr += "<?xml-stylesheet type=\"text/xsl\" href=\""+Application.StartupPath+"\\style\\OrderForm.xsl\"?>";
			xmlstr += "<order>\n";
			xmlstr += "<id>" + orderProdDetails[0] + "</id>\n";
			xmlstr += "<date>" + DateTime.Today + "</date>\n";
			xmlstr += "<customer>\n";
			xmlstr += "<name>" + orderProdDetails[1] + "</name>\n";
			xmlstr += "<address>" + " -- " + "</address>\n";
			xmlstr += "</customer>\n";
			xmlstr += "<transporter>\n";
			xmlstr += "<name>" + orderProdDetails[3] + "</name>\n";
			xmlstr += "</transporter>\n";
			xmlstr += "<lrno>" + orderProdDetails[4] + "</lrno>\n";
			xmlstr += "<products>\n";
			foreach(DataRow drox in dsOrderHistory.Tables[0].Rows)
			{
				xmlstr += "<product>\n";
				xmlstr += "<quantity>" + drox["Quantity"].ToString() + "</quantity>\n";
				xmlstr += "<name>" + drox["Product_Name"].ToString() + "</name>\n";
				xmlstr += "<rate>" + drox["Price"].ToString() + "</rate>\n";
				xmlstr += "<amount>" + drox["QUANTITY_PRICE"].ToString() + "</amount>\n";
				xmlstr += "</product>\n";
			}
			xmlstr += "</products>\n";
			xmlstr += "</order>";

			xdoc = new XmlDataDocument();
			xdoc.LoadXml(xmlstr);
			xdoc.Save(Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)+ "\\RPOrder_Details"+orderProdDetails[0]+".xml");
			//xdoc.Save("C:\\challan\\RPOrder_Details"+orderProdDetails[0]+".xml");
			
		}
		void BtnOKClick(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		
		void BtnCancelClick(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		void BtnPrintClick(object sender, EventArgs e)
		{
			
			generateXML(orderProductsDetails);
			XslCompiledTransform xslt = new XslCompiledTransform();

			xslt.Load(Application.StartupPath+"\\style\\OrderForm.xsl");
			xslt.Transform(Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)+"\\RPOrder_Details"+orderProductsDetails[0]+".xml", "c:\\result1.html");
			//xslt.Transform("C:\\challan\\RPOrder_Details"+orderProductsDetails[0]+".xml", "c:\\result1.html");
			Process proc = new Process();
			ProcessStartInfo psi = proc.StartInfo;
			psi.FileName = "c:\\result1.html";
			proc.Start();
		
		/*	
			//dsCust.Tables[0].Rows[cboCustomers.SelectedIndex];
			//MessageBox.Show(cboCustomers.SelectedIndex.ToString());
			int custID = Int32.Parse(dsCust.Tables[0].Rows[cboCustomers.SelectedIndex].ItemArray[0].ToString());
			int transID = Int32.Parse(dsTrans.Tables[0].Rows[cboTransporters.SelectedIndex].ItemArray[0].ToString());
			int LRNO = Convert.ToInt32(txtLrno.Text);
			
			Order or = new Order();
			or.CustomerID = custID;
			or.TransporterID = transID;
			or.LRNo = LRNO;
			or.Products = ds;
			orderID = or.Add();
			
			
			if(orderID != 0)
			{
				MessageBox.Show("Order Created with Order Id : "+orderID);
			}
			else
			{
				MessageBox.Show("Sorry... Cannot Process Your Request");
			}
			btnPrint.Visible = true;
 */
		}
	}
}


